import { RoleVO } from "@/api/system/role/types";
import { request } from "@/utils/request";
import { UserForm, UserQuery, UserVO, UserInfoVO } from "./types";
import { parseStrEmpty } from "@/utils/ruoyi";

/**
 * 通过用户ids查询用户
 * @param userIds
 */
export const optionSelect = (
  userIds: (number | string)[]
): AxiosPromise<UserVO[]> => {
  return request({
    url: "/system/user/optionselect?userIds=" + userIds,
    method: "get",
  });
};

/**
 * 获取用户详情
 * @param userId
 */
export const getUser = (userId?: string | number): AxiosPromise<UserInfoVO> => {
  return request({
    url: "/system/user/" + parseStrEmpty(userId),
    method: "get",
  });
};

/**
 * 修改用户
 */
export const updateUser = (data: UserForm) => {
  return request({
    url: "/system/user",
    method: "put",
    data: data,
  });
};

/**
 * 用户密码重置
 * @param userId 用户ID
 * @param password 密码
 */
export const resetUserPwd = (userId: string | number, password: string) => {
  const data = {
    userId,
    password,
  };
  return request({
    url: "/system/user/resetPwd",
    method: "put",
    headers: {
      isEncrypt: true,
      repeatSubmit: false,
    },
    data: data,
  });
};

/**
 * 查询用户个人信息
 */
export const getUserProfile = (): AxiosPromise<UserInfoVO> => {
  return request({
    url: "/system/user/profile",
    method: "get",
  });
};

/**
 * 修改用户个人信息
 * @param data 用户信息
 */
export const updateUserProfile = (data: UserForm) => {
  return request({
    url: "/system/user/profile",
    method: "put",
    data: data,
  });
};

/**
 * 用户密码重置
 * @param oldPassword 旧密码
 * @param newPassword 新密码
 */
export const updateUserPwd = (oldPassword: string, newPassword: string) => {
  const data = {
    oldPassword,
    newPassword,
  };
  return request({
    url: "/system/user/profile/updatePwd",
    method: "put",
    headers: {
      isEncrypt: true,
      repeatSubmit: false,
    },
    data: data,
  });
};

/**
 * 用户头像上传
 * @param data 头像文件
 */
export const uploadAvatar = (data: FormData) => {
  return request({
    url: "/system/user/profile/avatar",
    method: "post",
    data: data,
  });
};

/**
 * 查询授权角色
 * @param userId 用户ID
 */
export const getAuthRole = (
  userId: string | number
): AxiosPromise<{ user: UserVO; roles: RoleVO[] }> => {
  return request({
    url: "/system/user/authRole/" + userId,
    method: "get",
  });
};

export default {
  getUser,
  optionSelect,
  updateUser,
  resetUserPwd,
  getUserProfile,
  updateUserProfile,
  updateUserPwd,
  uploadAvatar,
  getAuthRole,
};
